<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Models\Shop\Ticket;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class TicketController extends Controller
{
   /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
   public function index(Request $request)
   {
      $where = function ($query) use ($request) {
         if ($request->has('keyword') and $request->keyword != '') {
            $cate = $request->cate;
            $search = "%" . $request->keyword . "%";
            $query->where($cate, 'like', $search);
         }
      };

      $tickets = DB::table('tickets')->where($where)->paginate(config('admin.page_size'));
      $used = config('admin.ticket.used');  //使用范围
      $type = config('admin.ticket.type');  //是有类型
      $status = config('admin.ticket.status');  //状态

      return response()->json(compact('tickets', 'used', 'type', 'status'));
   }

   /**
    * Store a newly created resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @return \Illuminate\Http\Response
    */
   public function create(Request $request)
   {
      $used = config('admin.ticket.used');  //使用范围
      $type = config('admin.ticket.type');  //是有类型
      $status = config('admin.ticket.status');  //状态

      return response()->json(compact('used', 'type', 'status'));
   }

   /**
    * Store a newly created resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @return \Illuminate\Http\Response
    */
   public function store(Request $request)
   {
      $ticket = Ticket::create($request->all());
      if ($ticket) {
         $info = ['status' => 1, 'msg' => '新增优惠券成功~'];
         return response()->json($info);
      }
   }

   /**
    * Display the specified resource.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function show($id)
   {
      $ticket = Ticket::find($id);
      $used = config('admin.ticket.used');  //使用范围
      $type = config('admin.ticket.type');  //是有类型
      $status = config('admin.ticket.status');  //状态

      return response()->json(compact('ticket', 'used', 'type', 'status'));
   }


   /**
    * Update the specified resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function update(Request $request, $id)
   {
      $ticket = Ticket::find($id);
      $res = $ticket->update($request->all());
      if ($res) {
         $info = ['status' => 1, 'msg' => '更新优惠券成功~'];
         return response()->json($info);
      }
   }

   /**
    * Remove the specified resource from storage.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function destroy($id)
   {
      Ticket::destroy($id);
   }

   /***
    *  排序
    */
   public function change_sort(Request $request)
   {
      $ticket = Ticket::find($request->id);
      $ticket->sort_order = $request->sort_order;
      $ticket->save();
   }
}
